﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Codebricks.Db.Attributes;
using KBVault.DAL.Repository;
using Codebricks.Db.Interface;

namespace KBVault.DAL.Entities
{
    [MySqlTable("article_attachments")]
    public class Attachment:IMysqlTable
    {
        [MySqlField("Id")]
        [MySqlPrimaryKey(true)]
        [MySqlIgnoreOnUpdate]
        public long Id { get; set; }

        [MySqlField("article_id")]
        public int ArticleId { get; set; }

        [MySqlField("path")]
        public string Path { get; set; }

        [MySqlField("filename")]
        public string FileName { get; set; }

        [MySqlField("extension")]
        public string Extension { get; set; }

        [MySqlField("downloads")]
        public int Downloads { get; set; }

        // Hash and hashtime fields will be used for uploaded files before a new article is created
        // after N days, rows whose hash column is not null will be dropped
        
        [MySqlField("hash")]
        public string Hash { get; set; }

        [MySqlField("hash_time")]
        public DateTime HashTime { get; set; }

        public string Href
        {
            get
            {
                return String.Format("{0}/{1}.{2}", Path, Id, Extension);
            }
        }

        public Article Article
        {
            get
            {
                return ArticleId > 0 ? ArticleRepository.Get(ArticleId) : null;
            }
        }
    }
}
